//var path = "http://localhost:9140";	//api地址
var path = "http://" + window.location.host;

$.fn.serializeObject = function() {
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [ o[this.name] ];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};

$(function () {
   	$.ajaxInit();
});

(function($) {
    $.extend({
        layui_alert : function (msg, func) {
            layer.alert(msg, {
                title: '提示'
            });
        },

        layui_confirm : function(title, msg, submit_url, params) {
            layer.confirm(msg, {
                title: title,
                btn: ['确定','取消'] //按钮
            }, function() {
                var load_index = layer.load(1, {shade:0.4});
                $.ajax({
                    type: "POST",
                    dataType: "json",
                    url: submit_url ,
                    data: params,
                    success: function (result) {
                        if(result.result) {
                            $(".view-search-btn").click();
                        }
                        $.layui_alert(result.message);
                        layer.close(load_index);
                    },
                    error : function() {
                        $.layui_alert("提交请求异常！");
                        layer.close(load_index);
                    }
                });
            });
        },

        layui_confirm_to_deal: function(title, msg, func, cencel) {
        	var btn = ['确定'];
        	if(cencel) {
        		btn.push('取消');
        	}
            layer.confirm(msg, {
                title: title,
                btn: btn //按钮
            }, function() {
                func();
            });
        },
        
        /**
         * 固定列高度调整
         * @param {Object} tableElem
         */
        layui_table_auto_fixed : function(tableElem) {
        	var $tableView = tableElem || $(".layui-table-view");
        	var dataIndex ,trHeight;
        	$tableView.each(function() {
		        // 获取两侧浮动栏
		        var $fixed = $(this).find(".layui-table-fixed");
		        // 同步表头高度
		       	$fixed.find(".layui-table-header tr").height($(this).find(".layui-table-header tr").eq(0).height());
		        // 遍历tr 修正浮动栏行高
		        $(this).find(".layui-table-main tr").each(function() {
		            dataIndex = $(this).attr("data-index");
		            trHeight = $(this).css("height");
		            $fixed.find("tr[data-index=" + dataIndex + "]").css("height", trHeight);
		        });
	    	});
        },

        ajaxInit : function () {
            $.ajaxSetup({
                complete: function(xhr,status) {
                    var sessionStatus = xhr.getResponseHeader('sessionstatus');
                    if(xhr.status === 405) {
                    	top.location.href = path+"/main/login.html";
                    }
                }
            });
        },
        
        get_url_params : function(name) {
        	var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
			var r = window.location.search.substr(1).match(reg);
			return r !== null ? unescape(r[2]) : "";
        },
        
        export_file : function(urlStr, data) {
        	var tempForm = document.createElement("form");  
			tempForm.id="tempForm1";  
			tempForm.method="post";  
			tempForm.action=urlStr;  
			tempForm.target="_blank";
			for(var ke in data) {
				var hideInput = document.createElement("input");
				hideInput.type="hidden";
				hideInput.name= ke
				hideInput.value= data[ke];
				tempForm.appendChild(hideInput);
			}
			document.body.appendChild(tempForm);
			tempForm.submit();
			document.body.removeChild(tempForm);
        }
    });
})(jQuery);